package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysDictData;
import com.morningstar.system.service.SysDictDataService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 字典数据接口
 *
 * @author yanglianshan
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    private static final String PERMISSION = "system.dict.data";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysDictDataService dictDataService;

    /**
     * 字典数据列表
     *
     * @param commonQuery 查询条件
     * @return 字典数据列表
     */
    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysDictData>> list(@RequestBody CommonQuery<SysDictData, SysDictData> commonQuery) {
        SysDictData sysDictData = commonQuery.getQuery();
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        if (sysDictData != null) {
            queryWrapper.eq(StringUtils.isNotBlank(sysDictData.getDictType()), SysDictData::getDictType, sysDictData.getDictType());
            queryWrapper.like(StringUtils.isNotBlank(sysDictData.getDictLabel()), SysDictData::getDictLabel, sysDictData.getDictLabel());
            queryWrapper.like(StringUtils.isNotBlank(sysDictData.getDictValue()), SysDictData::getDictValue, sysDictData.getDictValue());
        }
        queryWrapper.orderByAsc(SysDictData::getDictSort);
        queryWrapper.orderByDesc(SysDictData::getCreateTime);
        return Result.successData(dictDataService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 添加字典数据
     *
     * @param sysDictData 字典数据
     * @return 添加结果
     */
    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysDictData sysDictData) {
        ServiceCheckResult checkResult = dictDataService.checkAddAndUpdate(sysDictData);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        dictDataService.save(sysDictData);
        return Result.successNoData();
    }

    /**
     * 更新字典数据
     *
     * @param sysDictData 字典数据
     * @return 更新结果
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysDictData sysDictData) {
        ServiceCheckResult checkResult = dictDataService.checkAddAndUpdate(sysDictData);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        dictDataService.updateById(sysDictData);
        return Result.successNoData();
    }

    /**
     * 删除字典数据
     *
     * @param dictCode 字典数据ID
     * @return 删除结果
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("dictCode") Long dictCode) {
        dictDataService.removeById(dictCode);
        return Result.successNoData();
    }

    /**
     * 字典数据详情
     *
     * @param dictCode 字典数据ID
     * @return 字典数据详情
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysDictData> detail(@RequestParam("dictCode") Long dictCode) {
        return Result.successData(dictDataService.getById(dictCode));
    }
}
